const httpRequest = (method,url, data) => {
    let httpDefaultOpts = {
        url: url,
        data: data,
        method: method,
        header: method == 'get' ? {
			'X-Requested-With': 'XMLHttpRequest',
			"Accept": "application/json",
			"Content-Type": "application/json; charset=UTF-8"
		} : {
			'X-Requested-With': 'XMLHttpRequest',
			'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
		},
		dataType: 'json',
	}
    let promise = new Promise(function(resolve, reject) {
        uni.request(httpDefaultOpts).then(
            (res) => {
                if(res[1].statusCode === 200){
                    resolve(res[1].data);
                }else{
                    httpCode(res);
                }
            }
        ).catch(
            (response) => {
                reject(response)
            }
        )
    })
    return promise
};

//带Token请求
const httpTokenRequest = (method,url,data) => {
    let token = uni.getStorageSync('token');
    //此token是登录成功后后台返回保存在storage中的
    let httpDefaultOpts = {
        url: url,
        data: data,
        method: method,
        header: method == 'get' ? {
		'X-Token': token,
        'X-Requested-With': 'XMLHttpRequest',
        "Accept": "application/json",
        "Content-Type": "application/json; charset=UTF-8"
    } : {
		'X-Token': token,
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/json; charset=UTF-8'
    },
        dataType: 'json',
    }
    let promise = new Promise(function(resolve, reject) {
        uni.request(httpDefaultOpts).then(
            (res) => {
                if(res[1].statusCode === 200){
                    resolve(res[1].data);
                }else{
                    httpCode(res);
                }
            }
        ).catch(
            (response) => {
                reject(response)
            }
        )
    })
    return promise
};

//带Token请求
const httpTokenRequestPut = (method,url,data) => {
    let token = uni.getStorageSync('token');
    //此token是登录成功后后台返回保存在storage中的
    let httpDefaultOpts = {
        url: url,
        data: data,
        method: method,
        header: method == 'get' ? {
		'X-Token': token,
        'X-Requested-With': 'XMLHttpRequest',
        "Accept": "application/json",
        "Content-Type": "application/json; charset=UTF-8"
    } : {
		'X-Token': token,
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
    },
        dataType: 'json',
    }
    let promise = new Promise(function(resolve, reject) {
        uni.request(httpDefaultOpts).then(
            (res) => {
                if(res[1].statusCode === 200){
                    resolve(res[1].data);
                }else{
                    httpCode(res);
                }
            }
        ).catch(
            (response) => {
                reject(response)
            }
        )
    })
    return promise
};
const httpTextPlainRequest = (method,url,data)=>{
    let httpDefaultOpts = httpDefault(method,url,data,{
        "Accept": "text/plain",
        "content-type":"text/plain"
    });
    let promise = new Promise(function(resolve, reject) {
        
        uni.request(httpDefaultOpts).then(
            (res) => {
                if(res[1].statusCode === 200){
                    resolve(res[1].data);
                }else{
                    httpCode(res);
                }
            }
        ).catch(
            (response) => {
                reject(response)
            }
        )
    })
    return promise
}
let httpCode = (res)=>{
    switch(res[1].statusCode){
        case 401:
            uni.showToast({
                icon: 'none',
                title: res[1].data.message
            });
            setTimeout(()=>{
                uni.navigateTo({
                    url: '/pages/login/login'
                });
            },500);
        break;
        case 417:
            uni.showToast({
                icon: 'none',
                title: res[1].data.message
            });
        break;
        default:
            uni.showToast({
                icon: 'none',
                title: res[1].data.message
            });
    }
}
let httpDefault=  (method,url,data,contentType)=> {
    return {
        url: url,
        data: data,
        method: method,
        header:contentType,
        dataType:'text'
    }
   
}
export default {
	httpRequest,
    httpTokenRequest,
	httpTokenRequestPut,
    httpTextPlainRequest
}



